package com.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    public String app_id = "9021000136615173";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCFB7jcvLLhnTdJI4cFLFYWp898x3NpeZ+C8zjpBYwSckWm7vyl6f1BhjgSRiiM07jANRQkMxJpOYdfeLqtzzKKGfOsq/PTH5HqJ0hcFlX+bW+mgacuTP8/apXg5benF1XkJ2caK/nPrpVr3OFp7p5iIf+77O1bV9VTcw6zdzuQQLMNCyq96OdKPA1wUpDrD0Yn1RudYHAfBovyNS8DtHBBSrbC2+jalF9KxTwThDSgcKWgRaa6L4r638CI5iIFp8Xgv9IC42+jm1ysMM70FxJXEyDWMcOoUB9YnVVKx5T3UZ5r/DIU86cXZW94KCFy1RrKgqxoq7FqkiWE9lJR41xPAgMBAAECggEARawpsdJSjb9deTzWNmtmGy+AiAWd+JvuYxMnt0NkfTTELg303KCUN4EoVMnR6w/oLyKdjegBuIKc1+/PNcDS42l8Ke9VIDnknhI8354DI+IFG9vDx4gkFv6nyMYHTcf3ACty4E4v5Am30Go61NRYt2S/H5mTJsf1vmMATBYiaAX5UCJ2MCVDAdDxFpoNH9Km4BHmwHq6nKc1t9OP+0Q3GWqRQGShj5dkkKPL+ZYypylThtcOl5hZ/bSbzw0/1SIPJ4fQxX/vna5WIC5IXJ4piNBJ/RJ1Ppj7G9fYWzbrBpF0IY5u0tZeytfYEKVX268YEFJi8Cv+I7KIFUUj8p5sIQKBgQC/k65teFBsy+9qnomEwaysB5lN56T4uzCesPZtcTaQc/EgCjwnd/IIywZXxIAvVvlivTrfkQvkZciCwXpPLGE5or6VHchAajC3VvaoAbLY46UwpNKX5rvKIYFXsXZo51eNJStvcfV9mRHxCsTamobsgttB0o2AKiUeEZLZPfOflQKBgQCxw+rgwQzVuwpavtj7BfmCRM2yizVukFz+mGD590wQkUhxy5WCDCPfhfpnn+/4ibogqrKM0RQ+r/OE/mVq84PLJh2yVsu63LW1PsJ06NsYInpbnNQCyX+dpP5OEQOPEXcE7tj90Qvu1QCQtiQOXNzgJpSRM5Ya8EnbcrF6ZHBjUwKBgDXvixUw4VuwEsiut8LTkByiywt+4xhQA0E6SzbGv/jtW36MwyDNiSvmlPOS1A//6Zhd9DxLJtD3wvrlI900fm7unb93shBIlUyYxDrKt6KOfMx0KwIvKMGgqzyYLuEb+K85PsBSki35MW7ufDubc88UjEw8zi++LdMIWZKl+Xf9AoGAVdaC7nCJ4BLnVxZc9CnYZ70Njq+F55OyNAYknc96XW2qC7kAwsFV+TNazPsAB4WJ6QhEG5dCLVsNPiURl+lqtIf9QwaCbgLxg0mA4K8oTKkpaF98SLQ/+UGYwt2YWFdYn3suMLHnDzavhTFOi9jvlxVKHCtKyktJfrwNU0p4sp0CgYADfx/1zSfpRpNtiGqomHKDb5c+8Y6TJ0RheoM7aWvQ7DjToqRmiPY4N6hNUj5gbvJVFMh4PH9N/1EFsHMVOLrE+vmJ7nh5j+QqChyoUJ7wjJcZ3IbVcCRhAzKnY+RN3CnKR/3jfYSkOMHZ6sWjAnduXku8ck6zAo0ePPnGYQ39mw==";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAt+ucxvBQ1flr8eXJaKUNioHwIHF1KxAD7UbT+aj5wjq1O7E68l/PR9IqF/oWzg8UeQuKVriVmyT5xdjH0sKuKr27wwJ64TJnm8LeZZlvXs82M47iu+ecoqNrdMbD0BmS8Ln6ENZ0JKm5ktc7I04FbRH296djjJFrGYhKrx6tIK1m4dmzibA0cECGWl5w7FCJMfl4yX4pOTuoA5SvM2pPqCkbCLk4B90SGePGDXWlbiFyn2/XDPyHzJEqFiZ1QafrjeV8cT/FsdYBoHX7EM7Ljsl1qpSxPJNcJyLDYZpIHQftjFWyMHE6Gp7IZdfgHPDTLTLvAAhGzYH7LwNv5J7j3QIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    public String notify_url = "http://127.0.0.1:8080/";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    public String return_url = "http://127.0.0.1:8080/";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    //订单超时时间
    private String timeout = "1m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    public String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
